package xm.chengdu.com.beijing_211;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.czy1121.view.RoundButton;
import com.suke.widget.SwitchButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import com.sdwfqin.cbt.Content;
import utils.FaSong;
import utils.SpfUtils;

public class Setting_Activity extends Activity {

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.textView4)
    TextView textView4;
    @BindView(R.id.fuwuqi)
    TextView fuwuqi;
    @BindView(R.id.Current)
    RelativeLayout Current;
    @BindView(R.id.Email)
    TextView Email;
    @BindView(R.id.lawn)
    RelativeLayout lawn;
    @BindView(R.id.Tel)
    TextView Tel;
    @BindView(R.id.pin)
    RelativeLayout pin;
    @BindView(R.id.switch_button)
    SwitchButton switchButton;
    @BindView(R.id.home)
    RoundButton home;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        boolean kaiguan = (boolean) SpfUtils.get(this, "kaiguan", false);
        if (kaiguan) {
            switchButton.setChecked(true);
        } else {
            switchButton.setChecked(false);
        }
        switchButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                if (!Content.isConn) {
                    Toast.makeText(Setting_Activity.this, "Please connect to Bluetooth first.", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (isChecked) {


                    String    kaijiqueren = FaSong.kaijiqueren("01");
                    MyEvent events4 = new MyEvent();
                    events4.setType("message");
                    events4.setContent(kaijiqueren);
                    EventBus.getDefault().post(events4);


                } else {
                    String    kaijiqueren = FaSong.kaijiqueren("00");
                    MyEvent events4 = new MyEvent();
                    events4.setType("message");
                    events4.setContent(kaijiqueren);
                    EventBus.getDefault().post(events4);

                }

            }
        });
    }

    @OnClick({R.id.img_back, R.id.Current, R.id.lawn, R.id.pin, R.id.home})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.Current:
                startActivity(new Intent(this, TimeActivity.class));

                break;
            case R.id.lawn:
                startActivity(new Intent(this, LawnActivity.class));

                break;
            case R.id.pin:
                startActivity(new Intent(this, Pin_CODERenActivity.class));

                break;
            case R.id.home:
                if (!Content.isConn) {
                    Toast.makeText(this, "Please connect to Bluetooth first.", Toast.LENGTH_SHORT).show();
                    break;
                }
                String kaijiqueren = FaSong.kaijiquerenxiafa();
                MyEvent events4 = new MyEvent();
                events4.setType("message");
                events4.setContent(kaijiqueren);
                EventBus.getDefault().post(events4);

                break;


        }
    }


    public void onEventMainThread(MyEvent event) {

        if (!TextUtils.isEmpty(event.getType())) {
            if (event.getType().equals("settingerr")) {
               finish();
            }


        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
